package mb.model;

import java.awt.Color;
import java.awt.Point;  // +
import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;
import javax.swing.JPanel;

public class Diagram extends JPanel implements MouseListener {

  // private Box box = new Box();  // -

  public Diagram() {
    super();
    // box.addMouseListener(this);  // -
    this.addMouseListener(this);  // +
    this.setBackground(Color.WHITE);
    this.setLayout(null);
    // this.add(box);  // -
  }

  /* {-
  public Box getBox() {
    return this.box;
  }
  -} */

  // {+
  private void addNewBox(Point aPoint) {
    Box newBox = new Box();
    newBox.setLocation(aPoint);
    newBox.addMouseListener(this);
    this.add(newBox);
    this.repaint();
  }
  // +}

  // {&
  public void mousePressed(MouseEvent e) {
    if (e.getSource() == this) {
      this.addNewBox(e.getPoint());
    }
    else if (e.getSource() instanceof Box) {
      Box clickedBox = (Box) (e.getSource());
      clickedBox.setSelected(!(clickedBox.isSelected()) );
    }
  }
  // &}

  public void mouseClicked(MouseEvent e) {}
  public void mouseReleased(MouseEvent e) {}
  public void mouseEntered(MouseEvent e) {}
  public void mouseExited(MouseEvent e) {}

}